package guiLayer;
import guiLayer.extensions.OwnerTreeModel;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import modelLayer.Owner;
import controlerLayer.OwnerCtr;


public class ReadOwner extends Reader {
	private static final long serialVersionUID = 1L;
	private ArrayList<Owner> owners;
	private OwnerTreeModel model;
	
	
	/**
	 * Create the panel.
	 */
	public ReadOwner() {
		txtItem1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				searchOwner(txtItem1.getText());
			}
		});
		owners = new ArrayList<Owner>();
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				searchOwner(txtItem1.getText());
			}
		});
		
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtItem1.setText("");
			}
		});
		
		lblItem.setText("Name");
		lblTitle.setText("Owner");
		panel_3.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Search Owner", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(51, 51, 51)));
		
		model = new OwnerTreeModel(owners);
		tree.setModel(model);		
	}

	private void searchOwner(final String name){
		Thread t = new Thread(){
			public void run(){
				OwnerCtr oCtr = new OwnerCtr();		
				model = new OwnerTreeModel(oCtr.searchOwners(name));
				tree.setModel(model);
			}
		};
		
		t.start();
	}
	
	@Override
	protected void updateData(){
		searchOwner(txtItem1.getText());
	}
}
